/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.app.controller;


import com.alibaba.fastjson.JSONObject;
import io.renren.common.exception.RRException;
import io.renren.common.utils.HttpTookit;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.app.entity.BalanceLogEntity;
import io.renren.modules.app.entity.SysAreaEntity;
import io.renren.modules.app.form.LoginForm;
import io.renren.modules.app.service.PublicService;
import io.renren.modules.app.service.SysAreaService;
import io.renren.modules.app.utils.JwtUtils;
import io.renren.modules.oss.cloud.OSSFactory;
import io.renren.modules.oss.entity.SysOssEntity;
import io.renren.modules.oss.service.SysOssService;
import io.renren.modules.sys.redis.SysConfigRedis;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.util.*;

/**
 * 公共开放接口
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/api/public")
@Api(tags = "APP公共开放接口")
public class PublicController {
    @Autowired
    private PublicService publicService;

    @Autowired
    private SysOssService sysOssService;

    @Resource
    protected SysConfigRedis sysConfigRedis;

    @Resource
    protected SysAreaService sysAreaService;


    /**
     * 发送短信
     */
    @GetMapping("/sendSms")
    @ApiOperation(value = "发送短信", notes = "传入mobile")
    public R sendSms(@RequestParam(name = "mobile",required = false, defaultValue = "0") String mobile){
        publicService.sendSms(mobile);
        return R.ok();
    }
    /**
     * 上传文件
     */
    @PostMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }
        //上传文件
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix);

        //保存文件信息
        SysOssEntity ossEntity = new SysOssEntity();
        ossEntity.setUrl(url);
        ossEntity.setCreateDate(new Date());
        sysOssService.save(ossEntity);

        return R.ok().put("url", url);
    }
    @ApiOperation("微信回调接口")
    @ApiIgnore
    @RequestMapping("/notify.wxpay")
    public String notify(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Access-Control-Allow-Origin", "*");
        return publicService.notifyPay(request, response);
    }

    @ApiOperation("alipay异步通知调用地址")
    @ApiIgnore
    @RequestMapping(value = "/getNotifyUrlInfo",method = RequestMethod.POST)
    public void alipayNotifyUrlInfo(HttpServletRequest request,HttpServletResponse response){
        publicService.aliNotify(request,response);
    }

    @GetMapping("/getSysConfig")
    @ApiOperation("获取系统配置信息")
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public R getSysConfig(String code){
        return R.ok().put("data", sysConfigRedis.get(code));
    }

    @GetMapping("/getAcUrl")
    @ApiOperation("远程get访问")
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public R getAcUrl(){
        String ret = HttpTookit.getRequest("http://nyst.u3h1.cn/api/open/sendSms");
        Map<String,Object> map = JSONObject.parseObject(ret,Map.class);
        boolean contains=map.containsKey("errno");
        if(contains && map.get("errno").toString().equals("0")){
            return R.ok().put("data",map);
        }else {
            return R.error();
        }
    }

    @GetMapping("/postAcUrl")
    @ApiOperation("远程post访问")
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public R postAcUrl(){
        Map<String,String> params = new HashMap<>();
        params.put("mobile","18380801412");
        String ret = HttpTookit.postRequest("http://nyst.u3h1.cn/api/open/sendSms",params);
        Map<String,Object> map = JSONObject.parseObject(ret,Map.class);
        boolean contains=map.containsKey("errno");
        if(contains && map.get("errno").toString().equals("0")){
            return R.ok().put("data",map);
        }else {
            return R.error();
        }
    }

    @ApiOperation(value = "查询地址树", response = SysAreaEntity.class)
    @GetMapping(value = "/getAddress")
    @ResponseBody
    public R getAddress() {
        List<SysAreaEntity> list = sysAreaService.getAddress();
        return R.ok().put("data",list);
    }
}
